package com.pink.task;

import com.pink.component.RedisComponent;
import com.pink.entity.config.Constants;
import com.pink.entity.query.VideoFileQuery;
import com.pink.service.VideoFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
@Slf4j
public class ExecuteQueueTask {
    private ExecutorService executorService = Executors.newFixedThreadPool(Constants.LENGTH_2);

    @Resource
    private RedisComponent redisComponent;

    @Resource
    private VideoFileService videoFileService;

    @PostConstruct
    public void consumTrasnferFileQueues(){
        executorService.execute(()->{
            while (true){
                VideoFileQuery videoFilequery = null;
                try{
                    //消费队列
                    videoFilequery = redisComponent.getFileFromTransferQueue();
                    if(videoFilequery == null){
                        Thread.sleep(1500);
                        continue;
                    }
                    videoFileService.transferVideoFile(videoFilequery);
                }catch (Exception e){
                    log.error("获取转码文件队列异常",e);
                }
            }
        });
    }
}
